package com.zyk.scaffold.seata.biz.business.buy.service.impl;

import com.zyk.scaffold.common.utils.AssertUtil;
import com.zyk.scaffold.core.domain.Result;
import com.zyk.scaffold.seata.biz.business.buy.pojo.request.BuyRequest;
import com.zyk.scaffold.seata.biz.business.buy.service.BuyService;
import com.zyk.scaffold.seata.biz.feign.order.OrderFeignClient;
import com.zyk.scaffold.seata.biz.feign.order.request.OrderRequest;
import com.zyk.scaffold.seata.biz.feign.stock.StockFeignClient;
import com.zyk.scaffold.seata.biz.feign.stock.request.StockRequest;
import com.zyk.scaffold.seata.biz.repositorys.mapper.BizMoneyMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BuyServiceImpl implements BuyService {

    @Resource
    private OrderFeignClient orderFeignClient;

    @Resource
    private StockFeignClient stockFeignClient;

    @Resource
    private BizMoneyMapper bizMoneyMapper;

    @Override
    @GlobalTransactional
    public Boolean ok(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.ok(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.ok(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        return Boolean.TRUE;
    }

    @Override
    @GlobalTransactional
    public Boolean biz_error(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.ok(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.ok(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        AssertUtil.error("支付失败！");
        return Boolean.TRUE;
    }

    @Override
    @GlobalTransactional
    public Boolean order_error(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.error(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.ok(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        return Boolean.TRUE;
    }

    @Override
    @GlobalTransactional
    public Boolean stock_error(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.ok(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.error(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean none_seata_biz_error(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.none_seata_ok(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.none_seata_ok(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        AssertUtil.error("支付失败！");
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean none_seata_order_error(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.none_seata_error(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.none_seata_ok(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean none_seata_stock_error(BuyRequest buyRequest) {
        Long userId = Long.valueOf(buyRequest.getUserId());
        int useMoney = buyRequest.getCount() * 10;
        // 创建订单
        Result<Boolean> order = orderFeignClient.none_seata_ok(OrderRequest.of(buyRequest, useMoney));
        AssertUtil.isTrue(order.getData(), "订单创建失败！");
        // 减库存
        Result<Boolean> stock = stockFeignClient.none_seata_error(StockRequest.of(buyRequest));
        AssertUtil.isTrue(stock.getData(), "减库存失败！");
        // 减钱包
        Boolean pay = bizMoneyMapper.pay(userId, useMoney) > 0;
        AssertUtil.isTrue(pay, "支付失败！");
        return Boolean.TRUE;
    }
}
